package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.jdbc.IncorrectResultSetColumnCountException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author zhouwei
 * @date 2020-01-17 9:24
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            String queryString = queryPageBean.getQueryString();
            if(queryString!=null&&queryString.length()!=0) {
                String s = queryString.replaceAll(" ", "");
                queryPageBean.setQueryString(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageResult pageResult=roleService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 查询菜单
     * @return
     */
    @RequestMapping("/findMenu")
    public Result findMenu(){
        List<Menu> menuList = roleService.findMenu();
        return new Result(true,"",menuList);
    }

    /**
     * 新增角色
     * @param role
     * @param menuIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] menuIds){
        roleService.add(role,menuIds);
        return new Result(true,"新增角色成功");
    }

    /**
     * 查询单个id
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Map data= null;
        try {
            data = roleService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true,"查询角色成功",data);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] menuIds){
        try {
            roleService.edit(role,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true,"");
    }
}
